package www.admin.files;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class AdminFiles implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String url = request.getRequestURI();
		if(url.lastIndexOf("/login") !=-1 || url.lastIndexOf("/userlogin") !=-1 || url.lastIndexOf(".") !=-1){
			  return true;
		}
		Object session = request.getSession(true).getAttribute("user");
		if(session == null){
			  url =  request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
			  response.sendRedirect(url);
			  return true;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}
